var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'kxq123',
    database: 'mysite'
});

connection.connect();


/** Insert */
function insert(password, name, age) {
    var addSql = 'INSERT INTO user(password, name, age) VALUES(?,?,?)';
    var addSqlParams = [password, name, age];
    connection.query(addSql, addSqlParams, function (err, result) {
        if (err) {
            console.log('[INSERT ERROR] - ', err.message);
            return;
        }

        console.log('--------------------------INSERT----------------------------');
        //console.log('INSERT ID:',result.insertId);        
        console.log('INSERT ID:', result);
        console.log('-----------------------------------------------------------------\n\n');
    });

}

//查
function query() {
    var sql = 'SELECT * FROM user';
    connection.query(sql, function (err, result) {
        if (err) {
            console.log('[SELECT ERROR] - ', err.message);
            return;
        }

        console.log('--------------------------SELECT----------------------------');
        console.log(result);
        console.log('------------------------------------------------------------\n\n');
    });

}


function deleteItem() {
    var delSql = 'DELETE FROM user where id > 5';
    //删
    connection.query(delSql, function (err, result) {
        if (err) {
            console.log('[DELETE ERROR] - ', err.message);
            return;
        }

        console.log('--------------------------DELETE----------------------------');
        console.log('DELETE affectedRows', result.affectedRows);
        console.log('-----------------------------------------------------------------\n\n');
    });
}

function updateItem() {
    var modSql = 'UPDATE user SET name = ?,age = ? WHERE Id = ?';
    var modSqlParams = ['jjjjjjj', 18, 2];
    //改
    connection.query(modSql, modSqlParams, function (err, result) {
        if (err) {
            console.log('[UPDATE ERROR] - ', err.message);
            return;
        }
        console.log('--------------------------UPDATE----------------------------');
        console.log('UPDATE affectedRows', result.affectedRows);
        console.log('-----------------------------------------------------------------\n\n');
    });
}

// insert("ags", "keke", 67)
// query()
// deleteItem()
updateItem()

connection.end();